//
//  ViewController.m
//  25-HomeWork
//
//  Created by GivenBmli on 16/8/18.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)GCD:(id)sender {
    // GCD:(Grand Central Dispatch 翻译就是 宏大的中央调度)苹果开发的一种多线程的解决方法, 整体非常高效易用, 是目前所有线程处理方式中最好的一种 .基于C语言，提供了非常多强大的函数
    //    串行(Serial)：
    //    一个任务执行完, 再执行下一个任务
    //    并发 (Concurrent)：
    //    多个任务同时执行(自动开启多个线程),只有在异步函数下才有效
    //    同步(Synchronous)：
    //    在当前线程中执行任务，不具备开启新线程的能力提交的任务在执行完成后才会返回同步函数: dispatch_sync()
    //    异步 (Asynchronous)：
    //    在新的线程中执行任务, 具备开启线程的能力在新线程中执行任务，具备开启新线程的能力提交的任务立刻返回，在后台队列中执行异步函数: dispatch_async()
    // 1.自定义一个GCD队列
    // 参数1:给当前的队列起一个名
    // 参数2:选择并行/串行队列
    // DISPATCH_QUEUE_CONCURRENT 并联
    // DISPATCH_QUEUE_SERIAL 串联 (默认NULL)
    dispatch_queue_t myQueue = dispatch_queue_create("郭宝", DISPATCH_QUEUE_CONCURRENT);
    // GCD来完成图片的异步加载
    // 在子线程去加载网络数据, 然后在主线程完成UI页面的刷新
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    // 功能跟主线程一样，通过dispatch_get_main_queue()来获取，提交到main queue的任务实际上都是在主线程执行的，所以这是一个串行队列
    dispatch_async(myQueue, ^{
        NSString *str = @"http://img4.duitang.com/uploads/blog/201602/05/20160205185029_ZKz2A.jpeg";
        NSURL *url = [NSURL URLWithString:str];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        // 回到主队列, 刷新控件
        dispatch_async(mainQueue, ^{
            self.myImageView.image = image;
        });
    });

}
-(void)asyncCONCURRENT{
    NSLog(@"star");
    //一个队列 为 每个任务开辟一个线程
    dispatch_queue_t queue = dispatch_queue_create("影音先锋", DISPATCH_QUEUE_CONCURRENT);
    //dispatch_queue_t queue = dispatch_queue_create("影音先锋", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:3];
        NSLog(@"CONCURRENT_work_1\n");
    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"CONCURRENT_work_2\n");
    });
    dispatch_async(queue, ^{
        NSLog(@"CONCURRENT_work_3");
    });
    NSLog(@"end");
}
- (IBAction)asyncCONCURRENT:(id)sender {
    NSLog(@"star");
    //一个队列 为 每个任务开辟一个线程
    dispatch_queue_t queue = dispatch_queue_create("影音先锋", DISPATCH_QUEUE_CONCURRENT);
    //dispatch_queue_t queue = dispatch_queue_create("影音先锋", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:3];
        NSLog(@"CONCURRENT_work_1\n");
    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"CONCURRENT_work_2\n");
    });
    dispatch_async(queue, ^{
        NSLog(@"CONCURRENT_work_3");
    });
    NSLog(@"end");

}
- (IBAction)asyncSERIAL:(id)sender {
    NSLog(@"star");
    // 会开辟新的线程,但是是串行执行任务
    dispatch_queue_t queue = dispatch_queue_create("影音先锋", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:3];
        NSLog(@"CONCURRENT_work_1\n");
    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"CONCURRENT_work_2\n");
    });
    dispatch_async(queue, ^{
        NSLog(@"CONCURRENT_work_3");
    });
    NSLog(@"end");

}

- (IBAction)syncCONCURRENT:(id)sender {
    // 不会开辟新的线程
    // 串行执行命令
    NSLog(@"star");
    dispatch_queue_t queue = dispatch_queue_create("影音先锋", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:3];
        NSLog(@"CONCURRENT_work_1\n");
    });
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"CONCURRENT_work_2\n");
    });
    dispatch_sync(queue, ^{
        NSLog(@"CONCURRENT_work_3");
    });
    NSLog(@"end");

}
- (IBAction)syncSERIAL:(id)sender {
    NSLog(@"star");
    // 不会开辟新的线程
    dispatch_queue_t queue = dispatch_queue_create("影音先锋", DISPATCH_QUEUE_SERIAL);
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:3];
        NSLog(@"SERIAL_work_1\n");
    });
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"SERIAL_work_2\n");
    });
    dispatch_sync(queue, ^{
        NSLog(@"SERIAL_work_3");
    });
    NSLog(@"end");

}
- (IBAction)asyncGroupNotify:(id)sender {
    NSLog(@"star");
    dispatch_group_t group=dispatch_group_create();
    dispatch_queue_t queue=dispatch_queue_create("com.GCD_demo.www", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_async(group, queue, ^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"group_work_1");
    });
    dispatch_group_async(group, queue, ^{
        [NSThread sleepForTimeInterval:6];
        NSLog(@"group_work_2");
    });
    dispatch_group_async(group, queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"group_work_3");
    });
    
    dispatch_group_notify(group, queue, ^{
        NSLog(@"dispatch_group_Notify 结束");
    });

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
